package com.flight.trans.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.adapter.UnusualListAdapter;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.bean.UnusualBean;
import com.flight.trans.bean.UnusualBean.UnusualData;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.OnItemClickListener;
import com.flight.trans.listener.OnTextClickListener;
import com.flight.trans.listener.UnusualConfirmClickListener;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.ToastUtils;
import com.flight.trans.view.dialog.AlertDialog;
import com.flight.trans.view.dialog.UnusualDialog;
import com.flight.trans.view.dialog.UnusualTypeDialog;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author : YanKingonBackPressed
 * @date : 2019/5/19  9:53
 * @Description : 确定取消弹框
 */
public class UnusualListActivity extends BaseActivity {

    private LinearLayout mLlContainer ;
    private Button mBtnConfirm ;//确认
    private TextView mTvTitleName ;
    private RecyclerView mRlContainer ;

    private TextView mTvUnusualGradle ;

    private UnusualListAdapter mUnusualAdapter ;
    private UnusualBean mUnusualBean ;
    private List<UnusualData> mUnusualBeanList;
    private boolean isCa ;
    private int mBusinessID ;
    private int mSourceType ;
    private int turnType ;
    private int mPosition ;

    @Override
    protected int setContentView() {
        return R.layout.activity_unusual_list;
    }

    @Override
    protected void initView() {
        mLlContainer = findViewById(R.id.mLlContainer);
        mBtnConfirm = findViewById(R.id.mBtnConfirm);
        mTvTitleName = findViewById(R.id.mTvTitleName);
        mTvUnusualGradle = findViewById(R.id.mTvUnusualGradle);
        mRlContainer = findViewById(R.id.mRlContainer);
    }

    @Override
    protected void initData() {
        IsEditor = mBundle.getBoolean(Constant.IsEditor);
        mBusinessID = mBundle.getInt(Constant.Business_ID);
        mSourceType = mBundle.getInt(Constant.Source_Type);
        isCa = !TextUtils.isEmpty(mBundle.getString(Constant.Key_Order_No)) && mBundle.getString(Constant.Key_Order_No).startsWith(ConstantInfo.CA_ORDER_NO_START);
        mUnusualBean = (UnusualBean) mBundle.getSerializable(Constant.INFO);
        turnType = mBundle.getInt(Constant.TurnType);
        mPosition = mBundle.getInt(Constant.Position);

        mTvUnusualGradle.setVisibility(isCa ? View.VISIBLE : View.GONE);
        mBtnConfirm.setVisibility(turnType != 0 ? View.VISIBLE : View.GONE);

        mTvTitleName.setText(getString(R.string.title_order_unusual));
        CommonUtils.onFinishTouch(mActivity , mLlContainer);
    }

    @Override
    protected void initConnect() {
        if(turnType == 1){
            mUnusualBeanList = mUnusualBean.getData();
            initAdapter();
            return;
        }

        valueMap = new HashMap<>();
        valueMap.put(Constant.Business_ID , mBusinessID);

        NetworkUtils.getUnusualDataList(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mUnusualBeanList = (List<UnusualData>) body;

                initAdapter();
            }
        }, valueMap);
    }

    private void initAdapter(){
        if(mUnusualBeanList == null)
            mUnusualBeanList = new ArrayList<>();

        if(mUnusualAdapter == null){
            mUnusualAdapter = new UnusualListAdapter(mActivity , mUnusualBeanList , isCa , turnType);
            mRlContainer.setAdapter(mUnusualAdapter);
            mRlContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            mUnusualAdapter.setOnTextClickListener(new ItemTextClickListener());
            mUnusualAdapter.setOnItemClickListener(new ItemOnClickListener());
        }else{
            mUnusualAdapter.updateAdapter(mUnusualBeanList);
        }
    }

    /**
     * 条目点击事件
     */
    private class ItemOnClickListener extends OnItemClickListener{
        @Override
        public void onClick(View view, int position) {
            UnusualDialog.getInstance(mActivity , isCa)
                    .setUnusualData(mUnusualBeanList.get(position) , position)
                    .setNetwork(turnType == 0)
                    .setConfirmButton(new ConfirmClickListener())
                    .show();
        }

        @Override
        public void onLongClick(View view, int position) {
            if(turnType == 0){
                if(ConstantInfo.UnusualIDList.contains(mUnusualBeanList.get(position).getId())){
                    AlertDialog.getInstance(mActivity)
                            .setContent(R.string.dialog_delete_unusual)
                            .setConfirmButton(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    NetworkUtils.deleteUnusual(new OkGoBackListener(mActivity){
                                        @Override
                                        public void onSuccess(Object body) {
                                            initConnect();
                                        }
                                    } , mUnusualBeanList.get(position).getId());
                                }
                            }).show();
                } else {
                    ToastUtils.showToast(R.string.toast_unusual_not_delete);
                }
            } else {
                AlertDialog.getInstance(mActivity)
                        .setContent(R.string.dialog_delete_unusual)
                        .setConfirmButton(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mUnusualBeanList.remove(position);
                                initAdapter();
                            }
                        }).show();
            }
        }
    }

    /**
     * 条目点击事件
     */
    private class ItemTextClickListener implements OnTextClickListener{
        @Override
        public void onClick(View view, int position) {
            switch (view.getId()){
                case R.id.mIvCheckImage:
                    if(mUnusualBeanList.get(position).getBizAnomalyRecordFileVOList() == null || mUnusualBeanList.get(position).getBizAnomalyRecordFileVOList().size() == 0){
                        ToastUtils.showToast(R.string.toast_unusual_not_image);
                    }else {
                        ArrayList<String> imageList = new ArrayList<>();
                        for (int i = 0; i < mUnusualBeanList.get(position).getBizAnomalyRecordFileVOList().size(); i++) {
                            if(mUnusualBeanList.get(position).getBizAnomalyRecordFileVOList().get(i) == null){
                                ToastUtils.showToast(R.string.toast_unusual_image_null);
                                initConnect();
                                return;
                            }

                            imageList.add(mUnusualBeanList.get(position).getBizAnomalyRecordFileVOList().get(i).getUrl());
                        }

                        Intent mIntent = new Intent(mActivity , SeePicActivity.class);
                        Bundle mBundle = new Bundle();
                        mBundle.putStringArrayList(Constant.INFO , imageList);
                        mBundle.putInt(Constant.POSITION , 0);
                        mIntent.putExtras(mBundle);
                        turnActForResult(mIntent);
                    }
                    break;
            }
        }
    }

    public void onClick(View view){
        if(CommonUtils.isFastDoubleClick())
            return;

        switch (view.getId()){
            case R.id.mBtnAdd:
                // 新增
                UnusualData unusualData = new UnusualData();
                unusualData.setBusinessId(mBusinessID);
                unusualData.setSourceType(mSourceType);

                UnusualTypeDialog.getInstance(mActivity , isCa)
                        .setUnusualData(unusualData , -1)
                        .setNetwork(turnType == 0)
                        .setConfirmClickListener(new ConfirmClickListener())
                        .show();
                break;
            case R.id.mBtnConfirm:
                // 确认
                UnusualBean unusualBean = new UnusualBean();
                unusualBean.setData(mUnusualBeanList);

                mBundle = new Bundle();
                mBundle.putSerializable(Constant.INFO , unusualBean);
                mBundle.putInt(Constant.Position , mPosition);
                onFinish(mBundle);
                break;
            case R.id.mIbBack:
                onFinish(Constant.RESULT_REFRESH_CODE);
                break;
        }
    }

    private class ConfirmClickListener implements UnusualConfirmClickListener {
        @Override
        public void onClick(UnusualData unusualData, int position) {
            if(turnType == 0) {
                initConnect();
            }else {
                if(position == -1) {
                    mUnusualBeanList.add(unusualData);
                }else {
                    mUnusualBeanList.set(position , unusualData);
                }
                initAdapter();
            }
        }
    }

    @Override
    public void onBackPressed() {
        onFinish(Constant.RESULT_REFRESH_CODE);
    }
}
